package weaver.interfaces.zwgk.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zwgk.utils.InvokeHelper;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-23
 * @Description: 批量收款接口
 * @ModifyLog:
 **/

public class WeaBatchProceedsAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {

        String requestid = requestInfo.getRequestid();
        String tablename =requestInfo.getRequestManager().getBillTableName();
        writeLog( ">>>>>>WeaBatchProceedsAction>>>>>" + requestid+"<br/>" );
        String resultText = "";
        RecordSet rs = new RecordSet();
        try {
            JSONArray proceedsArray = new JSONArray();
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            String qybh = Util.null2String( rs.getString( "qybh" ) );
            String zhbm = Util.null2String( rs.getString( "zhbm" ) );
            String cjr = Util.null2String( rs.getString( "cjr" ) );

            rs.executeQuery( "select * from " + tablename + "_dt1 where mainid=?  ", mainid );
            while (rs.next()) {
                String id = Util.null2String( rs.getString( "id" ) );
                JSONObject jsonData = new JSONObject();
                jsonData.put( "companyCode", qybh );
                jsonData.put( "contractCode", Util.null2String( rs.getString( "htbhwb" ) ) );
                jsonData.put( "proceedsWay", Util.null2String( rs.getString( "skfsd" ) ) );
                jsonData.put( "proceedsDate", Util.null2String( rs.getString( "srrq" ) ) );
                jsonData.put( "proceedsAmount", Util.null2String( rs.getString( "skje" ) ) );
                jsonData.put( "voucherNum", Util.null2String( rs.getString( "cwskpzh" ) ) );
                jsonData.put( "currency", Util.null2String( rs.getString( "skbz" ) ) );
                jsonData.put( "oppAcctNo", Util.null2String( rs.getString( "dfzh" ) ) );
                jsonData.put( "oppAcctName", Util.null2String( rs.getString( "dfhm" ) ) );
                jsonData.put( "oppBranch", Util.null2String( rs.getString( "dfkhx" ) ) );
                jsonData.put( "createBy", cjr );
                jsonData.put( "tenantCode", zhbm );
                jsonData.put( "status", "rkz" );
                proceedsArray.add( jsonData );
            }
                writeLog( ">>>>>>jsonData>>>>>" + proceedsArray.toJSONString());
                resultText = InvokeHelper.addBatchProceeds( proceedsArray.toJSONString() );
                writeLog( ">>>>>>resultText>>>>>" + resultText);
                JSONObject resultJson = JSONObject.parseObject( resultText );
                String code = Util.null2String( resultJson.getString( "code" ) );
                String message = Util.null2String( resultJson.getString( "message" ) );
                if (!"200".equals( code )) {
                    requestInfo.getRequestManager().setMessageid( "111111" );
                    requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + message );
                    return Action.FAILURE_AND_CONTINUE;
                }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
